package org.lex.dbq.view.metadata;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.lex.dbq.model.jdbc.resultset.AbstractResultSet;
import org.lex.dbq.view.sql.result.TableResultViewer;

public class SchemaDialog extends Dialog {
	private Connection con = null;

	public SchemaDialog(Shell shell, Connection con) {
		super(shell);
		this.con = con;
		this.setShellStyle(SWT.MAX | SWT.MIN | SWT.RESIZE);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Schema");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		GridData d = new GridData(GridData.FILL_BOTH);
		TableResultViewer pane = new TableResultViewer(parent, SWT.NONE);
		pane.setLayoutData(d);

		if (null != con) {
			try {
				pane.setResultSet(new AbstractResultSet() {
					@Override
					public ResultSet execute() throws SQLException {
						return con.getMetaData().getSchemas();
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return parent;
	}
}
